﻿import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:vischat/core/annotations/getx_route_enhanced.dart';
import 'package:vischat/core/constants/asset_paths.dart';
import 'package:vischat/generated/icons/app_icons.dart';
import 'package:vischat/modules/home/user/user_controller.dart';
import 'package:vischat/modules/home/user/widgets/user_posts/user_posts.dart';
import 'package:vischat/shared/widgets/avatar.dart';
import 'package:vischat/shared/widgets/buttons/custom_button.dart';

@GetXRoute('/user')
class UserPage extends GetView<UserController> {
  const UserPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SizedBox(
        height: double.infinity,
        child: Stack(
          children: [
            GetX<UserController>(
              builder:
                  (ctrl) => Container(
                    height: 400.h,
                    padding: EdgeInsets.all(60.w),
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage(AssetPaths.defaultUserCover),
                        fit: BoxFit.fill,
                      ),
                    ),
                    child: Column(
                      children: [
                        SizedBox(height: 40.h),
                        ctrl.isSelf
                            ? GestureDetector(
                              onTap: ctrl.handleMenu,
                              child: Align(
                                alignment: Alignment.centerRight,
                                child: CircleAvatar(
                                  radius: 30.sp,
                                  backgroundColor: const Color(0xFF43769B),
                                  child: const Icon(
                                    AppIcons.menu,
                                    color: Colors.white,
                                  ),
                                ),
                              ),
                            )
                            : SizedBox(),
                        SizedBox(height: 20.h),
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            AppAvatar(avatar: ctrl.userInfo?.avatar ?? ''),
                            SizedBox(width: 20.w),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  ctrl.userInfo?.name ?? '',
                                  style: TextStyle(
                                    fontSize: 32.sp,
                                    fontWeight: FontWeight.w600,
                                    color: Colors.white,
                                  ),
                                ),
                                Row(
                                  children: [
                                    _statistics(
                                      ctrl.userInfo?.likes_count,
                                      'page.user.like'.tr,
                                    ),
                                    SizedBox(width: 40.w),
                                    _statistics(
                                      ctrl.userInfo?.following_count,
                                      'page.user.follow'.tr,
                                    ),
                                    SizedBox(width: 40.w),
                                    _statistics(
                                      ctrl.userInfo?.followers_count,
                                      'page.user.fans'.tr,
                                    ),
                                  ],
                                ),
                                SizedBox(height: 20.h),
                                ctrl.isSelf
                                    ? Row(
                                      children: [
                                        CustomButton(
                                          text: 'button.edit_user'.tr,
                                          backgroundColor: Colors.white,
                                          onTap: ctrl.goUserDetail,
                                        ),
                                        SizedBox(width: 20.w),
                                        CustomButton(
                                          text: 'button.authentication'.tr,
                                          backgroundColor: Colors.white,
                                          onTap: ctrl.goAuthenticate,
                                        ),
                                      ],
                                    )
                                    : Row(
                                      children: [
                                        CustomButton(
                                          text: 'button.follow'.tr,
                                          onTap: ctrl.handleFollow,
                                        ),
                                        SizedBox(width: 10.w),
                                        CustomButton(
                                          text: 'button.talk'.tr,
                                          onTap: ctrl.goChat,
                                        ),
                                      ],
                                    ),
                              ],
                            ),
                            const Spacer(),
                            ctrl.isSelf
                                ? GestureDetector(
                                  onTap: ctrl.goVisitor,
                                  child: CircleAvatar(
                                    radius: 30.sp,
                                    backgroundColor: const Color(0xFF43769B),
                                    child: const Icon(
                                      AppIcons.group,
                                      color: Colors.white,
                                    ),
                                  ),
                                )
                                : Container(),
                          ],
                        ),
                      ],
                    ),
                  ),
            ),
            Positioned(
              top: 380.h,
              bottom: 0,
              left: 0,
              right: 0,
              child: UserPosts(
                key: ValueKey('user_posts_${controller.userInfo?.userId}'),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _statistics(int? count, String title) {
    return Column(
      children: [
        Text(
          '${count ?? 0}',
          style: TextStyle(fontSize: 28.sp, color: Colors.white),
        ),
        Text(title, style: TextStyle(fontSize: 28.sp, color: Colors.white)),
      ],
    );
  }
}
